﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Ajax.BLL;
using Newtonsoft.Json;

namespace Habito.Products
{
	public partial class Index : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!string.IsNullOrEmpty(Request.QueryString["id"]))
			{
				ViewState["id"] = Request.QueryString["id"];
			}

			ProductClass pclass = new ProductClass();
			Ajax.BLL.Products products = new Ajax.BLL.Products();
			List<Ajax.Model.ProductClass> list = new List<Ajax.Model.ProductClass>();
			list = pclass.GetModelList("");
			string header = @"<span class='menuheader expandable' style='color: #006FA1; font-size:11px; display:block; '>{0}</span>";
			StringBuilder result = new StringBuilder();
			List<Ajax.Model.ProductClass> list2 = new List<Ajax.Model.ProductClass>();

			List<Ajax.Model.Products> plists = new List<Ajax.Model.Products>();


			foreach (var item in list)
			{
				if (item.PID == null)
				{
					list2.Add(item);
				}
			}
			foreach (var pItem in list2)
			{
				result.Append(string.Format(header, pItem.Name));
				// classDetail
				result.Append("<ul class='categoryitems'>");
				string detailHtml = @"<li style='cursor:pointer;font-family:Arial, Helvetica, sans-serif; font-size:11px;'><span tag = '1' classID='{0}' class='link' >{1}</span></li>";
				List<Ajax.Model.Products> currentProduct = new List<Ajax.Model.Products>();
				currentProduct = products.GetModelList(" ClassID = " + pItem.ID);
				foreach (var item in currentProduct)
				{
					result.Append(string.Format(detailHtml, item.ID, item.ProductName));
				}
				result.Append("</ul>");
			}

			ViewState["class"] = result.ToString();
		}

		protected override void OnInit(EventArgs e)
		{
			if (Request.QueryString["GetProductDesciption"] == "1")
			{
				int productID = int.Parse(Request.QueryString["productID"]);
				Ajax.BLL.Products products = new Ajax.BLL.Products();
				Ajax.Model.Products p = new Ajax.Model.Products();
				p = products.GetModel(productID);
				if (p == null)
				{
					Response.Write("");
				}
				Response.Write(p.Description);
				Response.End();
			}
			if (Request.QueryString["GetAllProductByClass"] == "1")
			{

				int classID = int.Parse(Request.QueryString["classID"]);
				Ajax.BLL.Products products = new Ajax.BLL.Products();
				List<Ajax.Model.Products> list = new List<Ajax.Model.Products>();
				list = products.GetModelList(" ClassID = " + classID);
				Response.Write(JsonConvert.SerializeObject(list));
				Response.End();
			}
			base.OnInit(e);
		}
	}
}